/// @tags: CDQDiv
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

namespace BlueQuantum {

typedef long long LL;

int const N = 5e5 + 5;
LL const INF = 0x3f3f3f3f3f3f3f3fLL;

int n, hash[N], _pos_2d[N], _pos_1d[N];
LL ans;

struct Node {
  int x, y, z, w, id;
  bool ok;
  LL mx, val;
  inline bool operator==(Node const &rhs) const {
    return x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w;
  }
} a[N], tmp[N];

inline bool cmp1(Node const &lhs, Node const &rhs) {
  return lhs.x == rhs.x
             ? (lhs.y == rhs.y ? (lhs.z == rhs.z ? lhs.w < rhs.w : lhs.z < rhs.z)
                               : lhs.y < rhs.y)
             : lhs.x < rhs.x;
}

inline bool cmp2(Node const &lhs, Node const &rhs) {
  // return lhs.y == rhs.y ? (lhs.z == rhs.z ? lhs.w < rhs.w : lhs.z < rhs.z)
                        // : lhs.y < rhs.y;
  return lhs.y == rhs.y
             ? (lhs.z == rhs.z ? (lhs.w == rhs.w ? lhs.x < rhs.x : lhs.w < rhs.w)
                               : lhs.z < rhs.z)
             : lhs.y < rhs.y;
}

inline bool cmp3(Node const &lhs, Node const &rhs) {
  // return lhs.z == rhs.z ? lhs.w < rhs.w : lhs.z < rhs.z;
  return lhs.z == rhs.z
             ? (lhs.w == rhs.w ? (lhs.y == rhs.y ? lhs.x < rhs.x : lhs.y < rhs.y)
                               : lhs.w < rhs.w)
             : lhs.z < rhs.z;
}

struct BIT {
  LL bit[N];

  inline int lowbit(int x) { return x & (-x); }

  inline void modify(int x, LL d) {
    while (x <= n) {
      bit[x] = max(bit[x], d);
      x += lowbit(x);
    }
  }

  inline LL query(int x) {
    LL ans = -INF;
    while (x) {
      ans = max(ans, bit[x]);
      x -= lowbit(x);
    }
    return ans;
  }

  inline void clear(int x) {
    while (x <= n) {
      bit[x] = 0;
      x += lowbit(x);
    }
  }
} bit;

void CDQ2(int l, int r) {
  if (l == r) return;
  int mid = (l + r) >> 1;
  CDQ2(l, mid);
  sort(a + l, a + mid + 1, cmp3);
  sort(a + mid + 1, a + r + 1, cmp3);
  int j = l, i = mid + 1;
  while (i <= r) {
    while (j <= mid && a[j].z <= a[i].z) {
      if (a[j].ok) {
        bit.modify(a[j].w, a[j].mx);
      }
      ++j;
    }
    if (!a[i].ok) {
      a[i].mx = max(a[i].mx, bit.query(a[i].w) + a[i].val);
    }
    ++i;
  }
  for (int i = l; i < j; ++i) {
    if (a[i].ok) bit.clear(a[i].w);
  }
  for (int i = l; i <= r; ++i) {
    tmp[_pos_2d[a[i].id]] = a[i];
  }
  for (int i = l; i <= r; ++i) {
    a[i] = tmp[i];
  }
  CDQ2(mid + 1, r);
}

void CDQ1(int l, int r) {
  if (l == r) return;
  int mid = (l + r) >> 1;
  CDQ1(l, mid);
  for (int i = l; i <= mid; ++i) {
    a[i].ok = true;
  }
  for (int i = mid + 1; i <= r; ++i) {
    a[i].ok = false;
  }
  sort(a + l, a + r + 1, cmp2);
  for (int i = l; i <= r; ++i) {
    _pos_2d[a[i].id] = i;
  }
  CDQ2(l, r);
  for (int i = l; i <= r; ++i) {
    tmp[_pos_1d[a[i].id]] = a[i];
  }
  for (int i = l; i <= r; ++i) {
    a[i] = tmp[i];
  }
  CDQ1(mid + 1, r);
}

inline int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i].x >> a[i].y >> a[i].z >> a[i].w >> a[i].val;
    hash[i] = a[i].w;
  }
  sort(hash + 1, hash + n + 1);
  *hash = unique(hash + 1, hash + n + 1) - hash - 1;
  for (int i = 1; i <= n; ++i) {
    a[i].w = lower_bound(hash + 1, hash + *hash + 1, a[i].w) - hash;
  }
  sort(a + 1, a + n + 1, cmp1);
  int acnt = 0;
  for (int i = 1; i <= n; ++i) {
    if (a[i] == a[i - 1]) {
      a[acnt].val += max(0LL, a[i].val);
    } else {
      a[++acnt] = a[i];
    }
  }
  for (int i = 1; i <= n; ++i) a[i].mx = a[i].val, a[i].id = i, _pos_1d[a[i].id] = i;
  CDQ1(1, n);
  ans = -INF;
  for (int i = 1; i <= n; ++i) {
    ans = max(ans, a[i].mx);
  }
  cout << ans;
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("P5621 [DBOI2019] 德丽莎世界第一可爱.in", "r", stdin);
  freopen("P5621 [DBOI2019] 德丽莎世界第一可爱.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
